/**
 * 
 */
package com.thor.palmmall.rest.tenant;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.tenant.converter.ATenantConverter;
import com.thor.palmmall.rest.tenant.converter.STenantConverter;
import com.thor.palmmall.rest.tenant.converter.STenantQueryParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.tenant.STenant;
import com.thor.palmmall.spi.tenant.STenantService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class ATenantServiceImpl implements ATenantService {

  @Autowired
  private STenantService tenantService;

  @Override
  public QueryResult<ATenant> query(ATenantQueryParam queryParam) throws ThorServiceException {
    SResponse<SQueryResult<STenant>> resp = tenantService
        .query(STenantQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return SQueryResultConverter.convert(resp.getBody(), ATenantConverter.getInstance());
    } else {
      throw new ThorServiceException("无法查询商户：" + resp.getMessage());
    }
  }

  @Override
  public ATenant get(String id, AUserGroupLoadParam queryParam) throws ThorServiceException {
    SResponse<STenant> resp = tenantService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return ATenantConverter.getInstance().convert(resp.getBody());
    } else {
      throw new ThorServiceException("无法找到指定ID的商户信息：" + resp.getMessage());
    }
  }

@Override
public String save(ATenant tenant, BeanOperateInfo operateInfo) throws ThorServiceException {
    SResponse<String> resp = tenantService.save(
        STenantConverter.getInstance().convert(tenant),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("无法保存商户:" + resp.getMessage());
    }
}



}
